/**
 *	插件:短信使用
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */


package com.framework.Chajian;


import com.framerock.db.DBConn;
import com.framerock.server.Response;
import com.framerock.baselib.CLog;
import com.framerock.baselib.CallBack;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockHttp;
import com.framerock.baselib.RockMsg;
import com.framerock.baselib.Xinhu;

import java.util.Map;

public class ChajianSmsYunpian extends ChajianSms {

    private String sendurl = "https://sms.yunpian.com/v2/sms/batch_send.json";

    public ChajianSmsYunpian(DBConn dbs) {
        super(dbs);
    }

    public String sendCode(String mobile, String key)
    {
        Map<String, String> params = Rock.getMap();
        RockMsg codeobj = getCode(mobile, key);
        if(codeobj.isError())return codeobj.back();
        String code = codeobj.getData();
        params.put("code", code);
        return send(mobile, "", "您短信验证码为：#code#，请勿将验证码提供给他人。", params);
    }

    public String send(String mobile, String qianm, String smscont, Map<String, String> params)
    {
        String key = Xinhu.getVal(this.nDb, "yunpiankey");
        if(Rock.isEmpt(key))return Response.returnError("未设置云片网的apikey");

        if(Rock.isEmpt(qianm))qianm = Xinhu.getVal(this.nDb, "smsqianm");
        if(params!=null)for (Map.Entry<String, String> entry : params.entrySet()) {
            smscont = smscont.replace("#"+entry.getKey()+"#", entry.getValue());
        }

        String canshu = "apikey="+key+"";
        canshu += "&mobile="+mobile+"";
        canshu += "&text=【"+qianm+"】"+smscont+"";
        String url = this.sendurl;
        final String[] result = {""};
        RockHttp.sendurl(url,"POST",canshu, new CallBack(){
            @Override
            public void backhttp(int code, int arg1, String bstr) {
                result[0] = bstr;
            }
        });
        String cont = result[0];
        if(Rock.isEmpt(cont) || !cont.substring(0,1).equals("{")){
            cont = Response.returnError("yapierror");
        }else{
            if(Rock.contain(cont, "\"code\":0")){
                cont = Response.returnSuccess("ok");
            }else{
                cont = Response.returnError("yapierror1");
            }
        }
        return cont;
    }
}
